//
//  ThreadListData.m
//
//  Created by lianchuang  on 14-12-2
//  Copyright (c) 2014 __MyCompanyName__. All rights reserved.
//

#import "ThreadListData.h"


NSString *const kThreadListDataHighlight = @"highlight";
NSString *const kThreadListDataCollection = @"collection";
NSString *const kThreadListDataTid = @"tid";
NSString *const kThreadListDataReplies = @"replies";
NSString *const kThreadListDataSummary = @"summary";
NSString *const kThreadListDataTop = @"top";
NSString *const kThreadListDataNew = @"new";
NSString *const kThreadListDataIsrec = @"isrec";
NSString *const kThreadListDataSubject = @"subject";
NSString *const kThreadListDataClosed = @"closed";
NSString *const kThreadListDataAuthorid = @"authorid";
NSString *const kThreadListDataGood = @"good";
NSString *const kThreadListDataViews = @"views";
NSString *const kThreadListDataLastpost = @"lastpost";
NSString *const kThreadListDataPic = @"pic";
NSString *const kThreadListDataDateline = @"dateline";
NSString *const kThreadListDataGid = @"gid";
NSString *const kThreadListDataDisplayorder = @"displayorder";
NSString *const kThreadListDataTypeid = @"typeid";
NSString *const kThreadListDataIspic = @"ispic";
NSString *const kThreadListDataAuthor = @"author";
NSString *const kThreadListDataDigest = @"digest";


@interface ThreadListData ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ThreadListData

@synthesize highlight = _highlight;
@synthesize collection = _collection;
@synthesize tid = _tid;
@synthesize replies = _replies;
@synthesize summary = _summary;
@synthesize top = _top;
@synthesize aNew = _aNew;
@synthesize isrec = _isrec;
@synthesize subject = _subject;
@synthesize closed = _closed;
@synthesize authorid = _authorid;
@synthesize good = _good;
@synthesize views = _views;
@synthesize lastpost = _lastpost;
@synthesize pic = _pic;
@synthesize dateline = _dateline;
@synthesize gid = _gid;
@synthesize displayorder = _displayorder;
@synthesize typeid = _typeid;
@synthesize ispic = _ispic;
@synthesize author = _author;
@synthesize digest = _digest;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.highlight = [self objectOrNilForKey:kThreadListDataHighlight fromDictionary:dict];
            self.collection = [[self objectOrNilForKey:kThreadListDataCollection fromDictionary:dict] intValue];
            self.tid = [self objectOrNilForKey:kThreadListDataTid fromDictionary:dict];
            self.replies = [self objectOrNilForKey:kThreadListDataReplies fromDictionary:dict];
            self.summary = [self objectOrNilForKey:kThreadListDataSummary fromDictionary:dict];
            self.top = [self objectOrNilForKey:kThreadListDataTop fromDictionary:dict];
            self.aNew = [self objectOrNilForKey:kThreadListDataNew fromDictionary:dict];
            self.isrec = [self objectOrNilForKey:kThreadListDataIsrec fromDictionary:dict];
            self.subject = [self objectOrNilForKey:kThreadListDataSubject fromDictionary:dict];
            self.closed = [self objectOrNilForKey:kThreadListDataClosed fromDictionary:dict];
            self.authorid = [self objectOrNilForKey:kThreadListDataAuthorid fromDictionary:dict];
            self.good = [self objectOrNilForKey:kThreadListDataGood fromDictionary:dict];
            self.views = [self objectOrNilForKey:kThreadListDataViews fromDictionary:dict];
            self.lastpost = [self objectOrNilForKey:kThreadListDataLastpost fromDictionary:dict];
            self.pic = [self objectOrNilForKey:kThreadListDataPic fromDictionary:dict];
            self.dateline = [self objectOrNilForKey:kThreadListDataDateline fromDictionary:dict];
            self.gid = [self objectOrNilForKey:kThreadListDataGid fromDictionary:dict];
            self.displayorder = [self objectOrNilForKey:kThreadListDataDisplayorder fromDictionary:dict];
            self.typeid = [self objectOrNilForKey:kThreadListDataTypeid fromDictionary:dict];
            self.ispic = [self objectOrNilForKey:kThreadListDataIspic fromDictionary:dict];
            self.author = [self objectOrNilForKey:kThreadListDataAuthor fromDictionary:dict];
            self.digest = [self objectOrNilForKey:kThreadListDataDigest fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.highlight forKey:kThreadListDataHighlight];
    [mutableDict setValue:[NSNumber numberWithDouble:self.collection] forKey:kThreadListDataCollection];
    [mutableDict setValue:self.tid forKey:kThreadListDataTid];
    [mutableDict setValue:self.replies forKey:kThreadListDataReplies];
    [mutableDict setValue:self.summary forKey:kThreadListDataSummary];
    [mutableDict setValue:self.top forKey:kThreadListDataTop];
    [mutableDict setValue:self.aNew forKey:kThreadListDataNew];
    [mutableDict setValue:self.isrec forKey:kThreadListDataIsrec];
    [mutableDict setValue:self.subject forKey:kThreadListDataSubject];
    [mutableDict setValue:self.closed forKey:kThreadListDataClosed];
    [mutableDict setValue:self.authorid forKey:kThreadListDataAuthorid];
    [mutableDict setValue:self.good forKey:kThreadListDataGood];
    [mutableDict setValue:self.views forKey:kThreadListDataViews];
    [mutableDict setValue:self.lastpost forKey:kThreadListDataLastpost];
    NSMutableArray *tempArrayForPic = [NSMutableArray array];
    for (NSObject *subArrayObject in self.pic) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForPic addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForPic addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForPic] forKey:kThreadListDataPic];
    [mutableDict setValue:self.dateline forKey:kThreadListDataDateline];
    [mutableDict setValue:self.gid forKey:kThreadListDataGid];
    [mutableDict setValue:self.displayorder forKey:kThreadListDataDisplayorder];
    [mutableDict setValue:self.typeid forKey:kThreadListDataTypeid];
    [mutableDict setValue:self.ispic forKey:kThreadListDataIspic];
    [mutableDict setValue:self.author forKey:kThreadListDataAuthor];
    [mutableDict setValue:self.digest forKey:kThreadListDataDigest];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.highlight = [aDecoder decodeObjectForKey:kThreadListDataHighlight];
    self.collection = [aDecoder decodeDoubleForKey:kThreadListDataCollection];
    self.tid = [aDecoder decodeObjectForKey:kThreadListDataTid];
    self.replies = [aDecoder decodeObjectForKey:kThreadListDataReplies];
    self.summary = [aDecoder decodeObjectForKey:kThreadListDataSummary];
    self.top = [aDecoder decodeObjectForKey:kThreadListDataTop];
    self.aNew = [aDecoder decodeObjectForKey:kThreadListDataNew];
    self.isrec = [aDecoder decodeObjectForKey:kThreadListDataIsrec];
    self.subject = [aDecoder decodeObjectForKey:kThreadListDataSubject];
    self.closed = [aDecoder decodeObjectForKey:kThreadListDataClosed];
    self.authorid = [aDecoder decodeObjectForKey:kThreadListDataAuthorid];
    self.good = [aDecoder decodeObjectForKey:kThreadListDataGood];
    self.views = [aDecoder decodeObjectForKey:kThreadListDataViews];
    self.lastpost = [aDecoder decodeObjectForKey:kThreadListDataLastpost];
    self.pic = [aDecoder decodeObjectForKey:kThreadListDataPic];
    self.dateline = [aDecoder decodeObjectForKey:kThreadListDataDateline];
    self.gid = [aDecoder decodeObjectForKey:kThreadListDataGid];
    self.displayorder = [aDecoder decodeObjectForKey:kThreadListDataDisplayorder];
    self.typeid = [aDecoder decodeObjectForKey:kThreadListDataTypeid];
    self.ispic = [aDecoder decodeObjectForKey:kThreadListDataIspic];
    self.author = [aDecoder decodeObjectForKey:kThreadListDataAuthor];
    self.digest = [aDecoder decodeObjectForKey:kThreadListDataDigest];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_highlight forKey:kThreadListDataHighlight];
    [aCoder encodeDouble:_collection forKey:kThreadListDataCollection];
    [aCoder encodeObject:_tid forKey:kThreadListDataTid];
    [aCoder encodeObject:_replies forKey:kThreadListDataReplies];
    [aCoder encodeObject:_summary forKey:kThreadListDataSummary];
    [aCoder encodeObject:_top forKey:kThreadListDataTop];
    [aCoder encodeObject:_aNew forKey:kThreadListDataNew];
    [aCoder encodeObject:_isrec forKey:kThreadListDataIsrec];
    [aCoder encodeObject:_subject forKey:kThreadListDataSubject];
    [aCoder encodeObject:_closed forKey:kThreadListDataClosed];
    [aCoder encodeObject:_authorid forKey:kThreadListDataAuthorid];
    [aCoder encodeObject:_good forKey:kThreadListDataGood];
    [aCoder encodeObject:_views forKey:kThreadListDataViews];
    [aCoder encodeObject:_lastpost forKey:kThreadListDataLastpost];
    [aCoder encodeObject:_pic forKey:kThreadListDataPic];
    [aCoder encodeObject:_dateline forKey:kThreadListDataDateline];
    [aCoder encodeObject:_gid forKey:kThreadListDataGid];
    [aCoder encodeObject:_displayorder forKey:kThreadListDataDisplayorder];
    [aCoder encodeObject:_typeid forKey:kThreadListDataTypeid];
    [aCoder encodeObject:_ispic forKey:kThreadListDataIspic];
    [aCoder encodeObject:_author forKey:kThreadListDataAuthor];
    [aCoder encodeObject:_digest forKey:kThreadListDataDigest];
}

- (id)copyWithZone:(NSZone *)zone
{
    ThreadListData *copy = [[ThreadListData alloc] init];
    
    if (copy) {

        copy.highlight = [self.highlight copyWithZone:zone];
        copy.collection = self.collection;
        copy.tid = [self.tid copyWithZone:zone];
        copy.replies = [self.replies copyWithZone:zone];
        copy.summary = [self.summary copyWithZone:zone];
        copy.top = [self.top copyWithZone:zone];
        copy.aNew = [self.aNew copyWithZone:zone];
        copy.isrec = [self.isrec copyWithZone:zone];
        copy.subject = [self.subject copyWithZone:zone];
        copy.closed = [self.closed copyWithZone:zone];
        copy.authorid = [self.authorid copyWithZone:zone];
        copy.good = [self.good copyWithZone:zone];
        copy.views = [self.views copyWithZone:zone];
        copy.lastpost = [self.lastpost copyWithZone:zone];
        copy.pic = [self.pic copyWithZone:zone];
        copy.dateline = [self.dateline copyWithZone:zone];
        copy.gid = [self.gid copyWithZone:zone];
        copy.displayorder = [self.displayorder copyWithZone:zone];
        copy.typeid = [self.typeid copyWithZone:zone];
        copy.ispic = [self.ispic copyWithZone:zone];
        copy.author = [self.author copyWithZone:zone];
        copy.digest = [self.digest copyWithZone:zone];
    }
    
    return copy;
}


@end
